<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>广播给所有接受者</title>
    <script th:src="@{/js/jquery-3.0.0.js}"></script>
    <script th:src="@{/js/sockjs.min.js}"></script>
    <script th:src="@{/js/stomp.min.js}"></script>
</head>
<body>
<button id="stop">断开</button>
<hr/>
<div id="output"></div>
</body>
<script th:inline="javascript">

    //连接endpoint为"/ws/endpointChat"的节点,需要与后台配置一样
    var sock = new SockJS("/ws/endpointChat");
    var stomp = Stomp.over(sock);
    //连接WebSocket服务端
    stomp.connect({},function (frame) {
        //订阅/topic/hello发送的消息，这里与在控制器的messagingTemplate.convertAndSendToUser或者sendto中定义的订阅地址保持一致。
        stomp.subscribe("/topic/hello",receiveMsg);
    });

    /**收到消息*/
    function receiveMsg(message) {
        $('#output').append("<b>收到了:" + message.body + "</b><br/>")
    }

    /**断开*/
    $('#stop').click(function (){
        sock.close();
    });

</script>
</html>